package il.ac.braude.sona.gui.forms.questions;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;

import org.jdesktop.swingx.JXDatePicker;

/**
 * QDatePicker is a class adding the date picker jar utilities into our question system
 * @author  Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */
@SuppressWarnings("serial")
public class QDatePicker extends FormQuestion {
	/**
	 * the GUI utility for dates selections
	 */
	public JXDatePicker datePicker; 
	private JLabel l;
	/**
	 * parameter for date formating
	 */
	 public DateFormat dateFormat;
	 public Date date;
	 
	 /**
	  * creates a new date selection field with the given field as name
	  * sets a date format to use with the date
	  * @param field - name for the date picker
	  */
	@SuppressWarnings("deprecation")
	public QDatePicker(String field) {
		super(BoxLayout.X_AXIS);
		l = new JLabel(field);
		datePicker= new JXDatePicker(System.currentTimeMillis());
		dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		add(l);
		add(Box.createHorizontalGlue());
		add(datePicker);
	}

    /**
     * shows the date picker option
     */
	public void showButton() {
		datePicker.setVisible(true);
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public void setDefaultAnswer(Object defaultAnswer) {
		datePicker.setToday((Date) defaultAnswer);
	}

	@SuppressWarnings("deprecation")
	@Override
	public Object getAnswer() {
		return dateFormat.format(datePicker.getDate());
	}
}
